package com.itheima.controller;

import com.itheima.annotation.Log;
import com.itheima.domain.po.Dept;
import com.itheima.domain.vo.Result;
import com.itheima.service.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "部门管理", description = "部门管理相关接口")
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @Operation(summary = "查询所有部门", description = "获取系统中所有部门列表")
    @GetMapping
    public Result findAll() {
        log.info("查询所有部门数据");
        List<Dept> depts = deptService.list();
        return Result.success(depts);
    }

    @Operation(summary = "根据ID删除部门", description = "根据部门ID删除指定部门")
    @Log
    // 删除部门
    @DeleteMapping
    public Result deleteById(@Parameter(description = "部门ID") Integer id) {
        log.info("根据id删除部门，删除的部门id：{}", id);
        deptService.removeById(id);
        return Result.success();
    }

    @Operation(summary = "新增部门", description = "创建新的部门")
    @Log
    // 新增部门
    @PostMapping
    public Result save(@Parameter(description = "部门信息") @RequestBody Dept dept) {
        log.info("新增部门，要新增的部门信息：{}", dept);
        deptService.saveAndTime(dept);
        return Result.success();
    }

    @Operation(summary = "根据ID查询部门信息", description = "根据部门ID获取部门详细信息")
    // 根据ID查询部门信息
    @GetMapping("/{id}")
    public Result getById(@Parameter(description = "部门ID") @PathVariable Integer id) {
        log.info("根据id查询部门，要查询的id：{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @Operation(summary = "修改部门", description = "更新部门信息")
    @Log
    // 修改部门
    @PutMapping
    public Result update(@Parameter(description = "部门信息") @RequestBody Dept dept) {
        log.info("修改部门数据，修改后的部门数据：{}", dept);
        deptService.updateByIdAndTime(dept);
        return Result.success();
    }
}
